﻿#include "OperGraphPreprocess.h"
#include "OperGraphEvent3D.h"
#include "GraphDataVTKAdaptor/GraphVTKObject3D.h"
#include "GUIWidget/GraphArea.h"
#include "GUIWidget/GUIEnumType.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"

#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"

#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossMeshModel.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossNodesParts.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractRWall.h"
#include "FITK_Component/FITKRadiossData/FITKGravity.h"
#include "FITK_Component/FITKRadiossData/FITKInitialField.h"
#include "FITK_Component/FITKRenderWindowVTK/FITKGraph3DWindowVTK.h"

namespace GUIOper
{
    void OperGraphPreprocess::updateGraph(int dataObjId, bool forceUpdate)
    {
        //获取可视化窗口
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (!graphWidget)
        {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getGraphObjectsByDataId(dataObjId);

        //添加至三维可视化窗口
        for (Exchange::GraphVTKObject3D* obj : objs)
        {
            if (!obj)
            {
                continue;
            }

            obj->update(forceUpdate);
            addGraphObjectToWidget(obj, graphWidget, true);
        }
    }

    void OperGraphPreprocess::updateAll(bool forceUpdate)
    {
        updateAllModel(forceUpdate);
    }

    void OperGraphPreprocess::updateAllColor(int dataObjId, QColor color)
    {
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(dataObjId);

        //刷新颜色
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) {
                continue;
            }
            obj->updateModelColor(color);
        }

        // 刷新渲染。
        render();
    }
    void OperGraphPreprocess::updateAllModel(bool forceUpdate)
    {
        //获取可视化窗口
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) return;
        QList<Exchange::GraphVTKObject3D*>objs = {};

        Radioss::FITKRadiossCase* caseData = FITKAPP->getGlobalData()->getPhysicsData<Radioss::FITKRadiossCase>();
        if (caseData == nullptr) return;
        Radioss::FITKRadiossMeshModel* meshModel = caseData->getMeshModel();
        if (meshModel == nullptr) return;
        //获取所有几何
        Interface::FITKGeoCommandList* geoList = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        if (geoList)
        {
            for (int i = 0; i < geoList->getDataCount(); i++)
            {
                Interface::FITKAbsGeoCommand* geoCmd = geoList->getDataByIndex(i);
                if (geoCmd == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*>modelObjs = getGraphObjectsByDataId(geoCmd->getDataObjectID());
                objs << modelObjs;
            }
        }
        //获取所有部件
        Radioss::FITKRadiossPartManager* partMgr = meshModel->getPartsManager();
        if (partMgr) {
           for (int i = 0; i < partMgr->getDataCount(); i++)
            {
                Radioss::FITKRadiossPart* part = partMgr->getDataByIndex(i);
                if (part == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*> modeObjs = getGraphObjectsByDataId(part->getDataObjectID());
                objs << modeObjs;
            }
        }
        //获取所有刚性壁
        Radioss::FITKRadiossRWallManager* rwallMgr = caseData->getRWallManager();
        if (rwallMgr) {
            for (int i = 0; i < rwallMgr->getDataCount(); i++)
            {
                Radioss::FITKAbstractRWall* rwall = rwallMgr->getDataByIndex(i);
                if (rwall == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*> modeObjs = getGraphObjectsByDataId(rwall->getDataObjectID());
                objs << modeObjs;
            }
        }
        //获取所有的重力
        Radioss::FITKGravityManager* gravityMgr = caseData->getGravityManager();
        if (gravityMgr)
        {
            for (int i = 0; i < gravityMgr->getDataCount(); i++)
            {
                Radioss::FITKGravity* gravity = gravityMgr->getDataByIndex(i);
                if (gravity == nullptr) return;
                QList<Exchange::GraphVTKObject3D*> modeObjs = getGraphObjectsByDataId(gravity->getDataObjectID());
                objs << modeObjs;
            }
        }
        //获取所有的场
        Radioss::FITKInitialFieldManager* fieldMgr = caseData->getInitialFieldManager();
        if (fieldMgr)
        {
            for (int i = 0; i < fieldMgr->getDataCount(); i++)
            {
                Radioss::FITKAbstractInitialField* field = fieldMgr->getDataByIndex(i);
                if (field == nullptr) return;
                QList<Exchange::GraphVTKObject3D*> modeObjs = getGraphObjectsByDataId(field->getDataObjectID());
                objs << modeObjs;
            }
        }
        //添加至三维窗口
        for (Exchange::GraphVTKObject3D* obj : objs)
        {
            if (obj == nullptr) continue;
            obj->update(forceUpdate);
            addGraphObjectToWidget(obj, graphWidget, false);
        }
        graphWidget->reRender();
        graphWidget->fitView();
    }
    void OperGraphPreprocess::setModelVisible(int dataObjId, bool visibility, bool isFitView)
    {
        //获取或创建可视化对象
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) return;
        Comp::FITKGraph3DWindowVTK* renderWindow = graphWidget->getRenderWindow();
        if (renderWindow == nullptr) return;

        QList<Exchange::GraphVTKObject3D*> objs = getGraphObjectsByDataId(dataObjId);

        //隐藏对象
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) continue;
            obj->setVisible(visibility);
        }

        render();
        if (isFitView) {
            fitView();
        }
    }
    void OperGraphPreprocess::setPointVisible(bool visibility, bool isPart, bool isFitView)
    {
        //获取或创建可视化对象
        if (isPart)
        {
            Radioss::FITKRadiossCase* caseData = FITKAPP->getGlobalData()->getPhysicsData<Radioss::FITKRadiossCase>();
            if (caseData == nullptr) return;
            Radioss::FITKRadiossMeshModel* meshModel = caseData->getMeshModel();
            if (meshModel == nullptr) return;
            //获取所有部件
            Radioss::FITKRadiossPartManager* partMgr = meshModel->getPartsManager();
            if (partMgr) {
                for (int i = 0; i < partMgr->getDataCount(); i++)
                {
                    Radioss::FITKRadiossPart* part = partMgr->getDataByIndex(i);
                    if (part == nullptr) continue;
                    QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(part->getDataObjectID());
                    //隐藏对象
                    for (Exchange::GraphVTKObject3D* obj : objs) {
                        if (obj == nullptr) continue;
                        obj->setPointVisibility(visibility);
                    }
                }
            }
        }
        else
        {
            Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->
                getGeometryData<Interface::FITKGeoCommandList>();
            if (geometryData == nullptr) return;
            //获取所有的几何体
            for (int i = 0; i < geometryData->getDataCount(); i++)
            {
                Interface::FITKAbsGeoCommand* geoCmd = geometryData->getDataByIndex(i);
                if (geoCmd == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*> objs = getGraphObjectsByDataId(geoCmd->getDataObjectID());
                //隐藏对象
                for (Exchange::GraphVTKObject3D* obj : objs) {
                    if (obj == nullptr) continue;
                    obj->setPointVisibility(visibility);
                }
            }
        }

        render();
        if (isFitView) {
            fitView();
        }
    }

    void OperGraphPreprocess::setLineVisible(bool visibility, bool isPart, bool isFitView)
    {
        //获取或创建可视化对象
        if (isPart)
        {
            Radioss::FITKRadiossCase* caseData = FITKAPP->getGlobalData()->getPhysicsData<Radioss::FITKRadiossCase>();
            if (caseData == nullptr) return;
            Radioss::FITKRadiossMeshModel* meshModel = caseData->getMeshModel();
            if (meshModel == nullptr) return;
            //获取所有部件
            Radioss::FITKRadiossPartManager* partMgr = meshModel->getPartsManager();
            if (partMgr) {
                for (int i = 0; i < partMgr->getDataCount(); i++)
                {
                    Radioss::FITKRadiossPart* part = partMgr->getDataByIndex(i);
                    if (part == nullptr) continue;
                    QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(part->getDataObjectID());
                    //隐藏对象
                    for (Exchange::GraphVTKObject3D* obj : objs) {
                        if (obj == nullptr) continue;
                        obj->setLineVisibility(visibility);
                    }
                }
            }
        }
        else
        {
            Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->
                getGeometryData<Interface::FITKGeoCommandList>();
            if (geometryData == nullptr) return;
            //获取所有的几何体
            for (int i = 0; i < geometryData->getDataCount(); i++)
            {
                Interface::FITKAbsGeoCommand* geoCmd = geometryData->getDataByIndex(i);
                if (geoCmd == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*> objs = getGraphObjectsByDataId(geoCmd->getDataObjectID());
                //隐藏对象
                for (Exchange::GraphVTKObject3D* obj : objs) {
                    if (obj == nullptr) continue;
                    obj->setLineVisibility(visibility);
                }
            }
        }

        render();
        if (isFitView) {
            fitView();
        }
    }

    void OperGraphPreprocess::setAllModelVisible(bool visibility, bool isPart, bool isFitView)
    {
        //获取或创建可视化对象
        if (isPart)
        {
            Radioss::FITKRadiossCase* caseData = FITKAPP->getGlobalData()->getPhysicsData<Radioss::FITKRadiossCase>();
            if (caseData == nullptr) return;
            Radioss::FITKRadiossMeshModel* meshModel = caseData->getMeshModel();
            if (meshModel == nullptr) return;
            //获取所有部件
            Radioss::FITKRadiossPartManager* partMgr = meshModel->getPartsManager();
            if (partMgr) {
                for (int i = 0; i < partMgr->getDataCount(); i++)
                {
                    Radioss::FITKRadiossPart* part = partMgr->getDataByIndex(i);
                    if (part == nullptr) continue;
                    QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(part->getDataObjectID());
                    //隐藏对象
                    for (Exchange::GraphVTKObject3D* obj : objs) {
                        if (obj == nullptr) continue;
                        obj->setCellVisible(visibility);
                    }
                }
            }
        }
        else
        {
            Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->
                getGeometryData<Interface::FITKGeoCommandList>();
            if (geometryData == nullptr) return;
            //获取所有的几何体
            for (int i = 0; i < geometryData->getDataCount(); i++)
            {
                Interface::FITKAbsGeoCommand* geoCmd = geometryData->getDataByIndex(i);
                if (geoCmd == nullptr) continue;
                QList<Exchange::GraphVTKObject3D*> objs = getGraphObjectsByDataId(geoCmd->getDataObjectID());
                //隐藏对象
                for (Exchange::GraphVTKObject3D* obj : objs) {
                    if (obj == nullptr) continue;
                    obj->setCellVisible(visibility);
                }
            }
        }

        render();
        if (isFitView) {
            fitView();
        }
    }

    void OperGraphPreprocess::hightObject(int dataObjId, QColor color)
    {
        //获取可视化窗口
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) return;
        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs;
        objs = getGraphObjectsByDataId(dataObjId);
        if (objs.isEmpty()) return;

        //添加至三维窗口
        for (Exchange::GraphVTKObject3D* obj : objs)
        {
            if (obj == nullptr || obj->isHighlighting() == true) continue;

            obj->highlight(color);
            addGraphHightlightObjectToWidget(obj, graphWidget);


        }
    }
    void OperGraphPreprocess::clearAllHight()
    {
        // 获取可视化窗口。
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (!graphWidget) return;

        QList<Exchange::GraphVTKObject3D*> objs = getAllGraphObject();
        if (objs.isEmpty())return;

        // 添加至三维窗口。
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr || obj->isHighlighting() == false) continue;

            obj->disHighlight();
            addGraphObjectToWidget(obj, graphWidget);
        }
    }
    void OperGraphPreprocess::updatePointSize(int dataObjId, int pointSize)
    {
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(dataObjId);

        //刷新颜色
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) {
                continue;
            }
            obj->updatePointSize(pointSize);
        }

        // 刷新渲染。
        render();
    }
    void OperGraphPreprocess::updateLineWidth(int dataObjId, int lineWidth)
    {
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(dataObjId);

        //刷新颜色
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) {
                continue;
            }
            obj->updateLineWidth(lineWidth);
        }

        // 刷新渲染。
        render();
    }
    void OperGraphPreprocess::updateOpacity(int dataObjId, double opacity)
    {
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(dataObjId);

        //刷新颜色
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) {
                continue;
            }
            obj->updateOpacity(opacity);
        }

        // 刷新渲染。
        render();
    }
    void OperGraphPreprocess::updateRepresentation(int dataObjId, int representation)
    {
        GUI::GraphArea* graphWidget = getGraphWidget(GUI::GraphAreaType::GATPre);
        if (graphWidget == nullptr) {
            return;
        }

        //获取或创建可视化对象
        QList<Exchange::GraphVTKObject3D*> objs = getPartGeoGraphObjectsByDataId(dataObjId);

        //刷新颜色
        for (Exchange::GraphVTKObject3D* obj : objs) {
            if (obj == nullptr) {
                continue;
            }
            obj->updateRepresentation(representation);
        }

        // 刷新渲染。
        render();
    }
}
